package com.qf.service.impl;

import com.qf.mapper.PictureMapper;
import com.qf.mapper.SchoolMapper;
import com.qf.pojo.Picture;
import com.qf.pojo.SchoolInfo;
import com.qf.service.SchoolService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    SchoolMapper schoolMapper;
    @Autowired
    PictureMapper pictureMapper;

    @Override
    public ResultMsg querySchoolInfo() {
        SchoolInfo schoolInfo = schoolMapper.querySchoolInfo();
        return ResultMsg.SUCCESS(schoolInfo);
    }

    @Override
    public ResultMsg updateSchoolInfo(SchoolInfo schoolInfo) {
        int i = schoolMapper.updateSchoolInfo(schoolInfo);
        return ResultMsg.SUCCESS();
    }

    @Override
    public ResultMsg queryPicture(Integer pictureId) {
        List<Picture> pictures = pictureMapper.queryAllPicture(pictureId);
        if (pictures.size() > 1) return ResultMsg.SUCCESS(pictures);
        else if (pictures.size() == 1) return ResultMsg.SUCCESS(pictures.get(0));
        else return ResultMsg.FAILED(201, "no data was found");
    }

    @Override
    public ResultMsg insertPicture(Picture picture) {
        picture.setPictureId(null);
        int i = pictureMapper.insertPicture(picture);
        if (i > 0) return ResultMsg.SUCCESS(picture);
        return ResultMsg.FAILED(201, "failed to insert picture");
    }

    @Override
    public ResultMsg updatePicture(Picture picture) {
        int i = pictureMapper.updatePicture(picture);
        return ResultMsg.SUCCESS(i);
    }

    @Override
    public ResultMsg deletePicture(Integer pictureId) {
        if (pictureId == null) return ResultMsg.FAILED(202, "pictureId can not be null");
        int i = pictureMapper.deletePicture(pictureId);
        if (i <= 0) return ResultMsg.FAILED(201, "delete picture failed");
        else return new ResultMsg(0, i + " row of data is deleted successfully", null, 0);
    }
}
